<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page import="java.util.Set"%>
<c:set var="title" scope="request">Sessions Monitoring</c:set>
<jsp:include page="/WEB-INF/jsp/inc/head.jsp" />

<%
if (request.getUserPrincipal() == null ){
	%>
	<p class="alert">
		<em>You must be logged in as an administrator to use this feature.</em>
	</p>
	<p>
		<a href="<%=request.getContextPath()%>/tools/profile">Go to Login page.</a>
	</p>
<% } else {
	try{
		session.invalidate();
		request.getSession(true);
		//weblogic.servlet.security.ServletAuthentication.weak("weblogic", "pcswladm01", request, response);
		Class vServletAuthentication = Class.forName("weblogic.servlet.security.ServletAuthentication");
		java.lang.reflect.Method vAuth = vServletAuthentication.getMethod("weak", new Class[]{String.class, String.class, HttpServletRequest.class, HttpServletResponse.class});
		vAuth.invoke(vServletAuthentication.getClass(), new Object[] {"system", "pcswladm01", request, response});
		
		if (request.getUserPrincipal() != null ){
			out.print("request.getUserPrincipal().getName() = " + request.getUserPrincipal().getName()+ "<br>");
		} else {
			vAuth.invoke(vServletAuthentication.getClass(), new Object[] {"weblogic", "weblogic", request, response});
			if (request.getUserPrincipal() != null ){
				out.print("request.getUserPrincipal().getName() = " + request.getUserPrincipal().getName()+ "<br>");
			} else {
				out.print("request.getUserPrincipal().getName() = anonymous" + "<br>");
			}
		}
	
		String vSessionId = (String) request.getParameter("session");
		if (vSessionId != null ) {
			out.print("Suivi de la session: "+vSessionId+"<br>");
		}
		
		
		//vMBeanHome = weblogic.management.Admin.getInstance().getMBeanHome();
		Class vClass = Class.forName("weblogic.management.Admin");
		java.lang.reflect.Method vMethod= vClass.getMethod("getInstance", new Class[]{});
		Object vAdmin = vMethod.invoke(vClass.getClass(), null);
		vMethod = vAdmin.getClass().getMethod("getMBeanHome", new Class[]{});
		Object vMBeanHome = vMethod.invoke(vAdmin, null);
		if (vMBeanHome != null) {
			
			vMethod= vMBeanHome.getClass().getMethod("getAllMBeans", new Class[]{});
			Object vSet = vMethod.invoke(vMBeanHome, null);
			if (vSet != null && vSet instanceof java.util.Set) {
				java.util.Iterator vIt = ((java.util.Set)vSet).iterator();;
				while (vIt.hasNext()) {
					Object vObject = vIt.next();
					//out.print("++obj = " + vObject.getClass().getName() + "<br>");
					try {
						java.lang.reflect.Method vMethodgetServletSessionsMonitoringIds = vObject.getClass().getMethod("getServletSessionsMonitoringIds", null);
						java.lang.reflect.Method vMethodgetOpenSessionsCurrentCount = vObject.getClass().getMethod("getOpenSessionsCurrentCount", null);
						java.lang.reflect.Method vMethodgetOpenSessionsHighCount = vObject.getClass().getMethod("getOpenSessionsHighCount", null);
						java.lang.reflect.Method vMethodgetSessionCookieDomain = vObject.getClass().getMethod("getSessionCookieDomain", null);
						java.lang.reflect.Method vMethodgetSessionCookiePath = vObject.getClass().getMethod("getSessionCookiePath", null);
						java.lang.reflect.Method vMethodgetSessionCookieName = vObject.getClass().getMethod("getSessionCookieName", null);
						java.lang.reflect.Method vMethodgetSessionCookieMaxAgeSecs = vObject.getClass().getMethod("getSessionCookieMaxAgeSecs", null);
						java.lang.reflect.Method vMethodgetName = vObject.getClass().getMethod("getName", null);
						
						if (vMethodgetName!=null && vMethodgetServletSessionsMonitoringIds!=null && vMethodgetOpenSessionsHighCount!=null && vMethodgetOpenSessionsCurrentCount!=null) {
							try {
								Object vObjectResultgetName = vMethodgetName.invoke(vObject, null);
								Object vObjectOpenSessionsCurrentCount = vMethodgetOpenSessionsCurrentCount.invoke(vObject, null);
								Object vObjectOpenSessionsHighCount = vMethodgetOpenSessionsHighCount.invoke(vObject, null);
								Object vObjectgetSessionCookieDomain = vMethodgetSessionCookieDomain.invoke(vObject, null);
								Object vObjectgetSessionCookiePath = vMethodgetSessionCookiePath.invoke(vObject, null);
								Object vObjectgetSessionCookieName = vMethodgetSessionCookieName.invoke(vObject, null);
								Object vObjectgetSessionCookieMaxAgeSecs = vMethodgetSessionCookieMaxAgeSecs.invoke(vObject, null);
							
								Object vObjectResult = vMethodgetServletSessionsMonitoringIds.invoke(vObject, null);
								if (vObjectResultgetName != null && vObjectResult != null) {
									java.lang.String[] ServletSessionsMonitoringIds =  (java.lang.String[])vObjectResult;
									if (ServletSessionsMonitoringIds.length != 0) {
										StringBuffer vInterfaces = new StringBuffer();
										Class[] vClassInterface = vObject.getClass().getInterfaces();
										for (int k=0; k<vClassInterface.length;k++){
											vInterfaces.append(vClassInterface[k]);
										}
										out.print("---" + vObject.getClass().getName()+ " ("+vInterfaces.toString()+")." +vMethodgetName.getName()+" -> "+ vObjectResultgetName.getClass().getName() + " -> " + vObjectResultgetName.toString() + "<br>");
										out.print("--+ OpenSessionsCurrentCount -> " + vObjectOpenSessionsCurrentCount.toString() + "<br>");
										out.print("--+ OpenSessionsHighCount -> " + vObjectOpenSessionsHighCount.toString() + "<br>");
										out.print("--+ getSessionCookieDomain -> " + vObjectgetSessionCookieDomain + "<br>");
										out.print("--+ getSessionCookiePath -> " + vObjectgetSessionCookiePath + "<br>");
										out.print("--+ getSessionCookieName -> " + vObjectgetSessionCookieName + "<br>");
										out.print("--+ getSessionCookieMaxAgeSecs -> " + vObjectgetSessionCookieMaxAgeSecs + "<br>");
										
										String vMonitoringId = "";
										if (vSessionId!=null){
											try{
												java.lang.reflect.Method vgetMonitoringId = vObject.getClass().getMethod("getMonitoringId", new Class[]{String.class});
												vMonitoringId = (String) vgetMonitoringId.invoke(vObject, new String[] {vSessionId});
											} catch (Exception e){
											}
											if (vMonitoringId == null){
												vMonitoringId="";
											}
										}
										
										for (int i=0; i<ServletSessionsMonitoringIds.length;i++){
											try {
												java.lang.reflect.Method vMethodgetServletSession= vObject.getClass().getMethod("getSessionLastAccessedTime", new Class[]{String.class});
												Object vObjectServletSessionRuntimeMBean = vMethodgetServletSession.invoke(vObject, new String[] {ServletSessionsMonitoringIds[i].toString()});
												if (vObjectServletSessionRuntimeMBean != null) {
													if (vMonitoringId.compareTo(ServletSessionsMonitoringIds[i].toString()) == 0){
														out.print("*** Session suivi Id -> " + ServletSessionsMonitoringIds[i].toString() +" getSessionLastAccessedTime="+ vObjectServletSessionRuntimeMBean.toString() + "<br>");
													} else {
														out.print("--+ Session Id -> " + ServletSessionsMonitoringIds[i].toString() +" getSessionLastAccessedTime="+ vObjectServletSessionRuntimeMBean.toString() + "<br>");
													}
												} else{
													out.print("--+ Session Id -> " + ServletSessionsMonitoringIds[i].toString() + "<br>");
												}
											} catch (Exception e){
												out.print("--+ Exception: "+ e + "<br>");
												StackTraceElement[] vStackTraceElement = e.getStackTrace();
												for (int j=0; j<vStackTraceElement.length;j++){
													out.print(vStackTraceElement[j]+ "<br>");
												}											
											}
										}
									}
								}
							} catch (Exception e){
								out.print("-- Exception: "+ e + "<br>");
								StackTraceElement[] vStackTraceElement = e.getStackTrace();
								for (int i=0; i<vStackTraceElement.length;i++){
									out.print(vStackTraceElement[i]+ "<br>");
								}							
							}						
						}
						
					} catch (NoSuchMethodException e){
					}
					/*
					java.lang.reflect.Method[] vMethods =  vObject.getClass().getMethods();
					for (int i=0; i<vMethods.length;i++){
						//if (vMethods[i].getParameterTypes().length == 0 && ( vMethods[i].getName().startsWith("get") || vMethods[i].getName().startsWith("is") )) {
						if (vMethods[i].getParameterTypes().length == 0 && ( vMethods[i].getName().startsWith("getName") || vMethods[i].getName().startsWith("getSession") )) {
							try {
								Object vObjectResult = vMethods[i].invoke(vObject, null);
								if (vObjectResult != null) {
									out.print("---" + vObject.getClass().getName()+"." +vMethods[i].getName()+" -> "+ vObjectResult.getClass().getName() + " -> " + vObjectResult.toString() + "<br>");
								} else {
									out.print("---" + vObject.getClass().getName()+"." +vMethods[i].getName()+" -> null <br>");
								}
							} catch (Exception e){
								out.print("--" + vObject.getClass().getName()+" invoke " +vMethods[i].getName()+" with Exception: "+ e + "<br>");
							}
						} else {
							//out.print("--" + vObject.getClass().getName()+" not invoke " +vMethods[i].getName() + "<br>");
						}
							//
						//}
					}
					*/
				}
			}
		}	
		
	} catch (Exception e) {
		out.print("++++++++++++++++++++++++++++++<br>");
		out.print(e+ "<br>");
		StackTraceElement[] vStackTraceElement = e.getStackTrace();
		for (int i=0; i<vStackTraceElement.length;i++){
			out.print(vStackTraceElement[i]+ "<br>");
		}
	}
}
%>

<jsp:include page="/WEB-INF/jsp/inc/foot.jsp" />